﻿using System.Reflection;
using CoreEx.Common.Proxy;
using LinFu.IoC.Configuration;
using Mono.Cecil;

namespace CoreEx.Common.Implementation.Proxy
{
    /// <summary>
    /// Modifies the constructed method to include the parameter names.
    /// </summary>
    [Implements(typeof(IMethodModifier))]
    public class MethodParameterModifier : IMethodModifier
    {
        /// <summary>
        /// Fires before the proxy method is contructed.
        /// </summary>
        /// <param name="targetType">The constructed proxy type that will contain the constructed method.</param>
        /// <param name="method">The target method.</param>
        public void BeforeConstruction(TypeDefinition targetType, MethodInfo method)
        {            
        }

        /// <summary>
        /// Fires after the proxy method has been constructed.
        /// </summary>
        /// <param name="targetType">The constructed proxy type that contains the constructed method.</param>
        /// <param name="method">The target method.</param>
        /// <param name="methodDefinition">Represents the constructed proxy method.</param>
        public void AfterConstruction(TypeDefinition targetType, MethodInfo method, MethodDefinition methodDefinition)
        {            
            var parameters = method.GetParameters();
            for (int i = 0; i < parameters.Length; i++)
            {
                methodDefinition.Parameters[i].Name = parameters[i].Name;
            }
        }
    }
}